package com.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.entity.User;
import com.test.mapper.UserMapper;
import com.test.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper mapper;

    @GetMapping("/list")
    public IPage<User> getList() {
        IPage<User> page = new Page<>(1l, 5l);
        QueryWrapper<User> qw = new QueryWrapper<User>();
        qw.like("username", "admin");
        IPage<User> userIPage = mapper.selectPage(page, qw);
        return userIPage;
    }

    @GetMapping("/getUserById")
    public User getUserById(@RequestParam(defaultValue = "3") Long userid) {
        return userService.getUserInfoById(userid);
    }

    @Autowired
    private IUserService userService;

    @GetMapping("/listAll")
    public List<User> listAll() {
        List<User> users = userService.listAll();
        return users;
    }

    @GetMapping("/listOne")
    public User listOne() {
        return userService.listOne(1L);
    }

    //保存
    @GetMapping("/save")
    public void baocun1(int age, String name, String email) {
        User user1 = new User();
        user1.setUsername(name);
        userService.insert1(user1);
    }
}
